package com.zzq.my.jetpacks.jectpackapp.net.findBanner

import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.aisier.architecture.base.BaseViewModel
import com.aisier.network.observer.StateLiveData
import com.blankj.utilcode.util.ToastUtils
import com.zzq.my.jetpacks.jectpackapp.bean.BannerInfokt
import com.zzq.my.jetpacks.net.rxhttp.show
import kotlinx.coroutines.launch
import rxhttp.tryAwait

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2022/1/19-17:05
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class FindBannerViewModel : BaseViewModel() {
    private val mRepository by lazy { FindBannerRepo() }
    val bannerInfoLiveData = StateLiveData<List<BannerInfokt.Data>>()
    val bannerInfoLiveData2 = MutableLiveData<List<BannerInfokt.Data>>()
    val bannerInfoLiveData3 = MutableLiveData<List<BannerInfokt.Data>>()

    // 针对多数据源  比较典型的案例 一份数据既可以从网络上获取； 也可以从本地db,文件，sp 等获取
    val mediatorLiveDataLiveData = MediatorLiveData<List<BannerInfokt.Data>>().apply {
        addSource(bannerInfoLiveData3)
        {
            this.value = it
        }
        addSource(bannerInfoLiveData2)
        {
            this.value = it
        }
    }

    fun reqFindBanner() {
        //启动一个协程
        viewModelScope.launch {
            bannerInfoLiveData.value = mRepository.findBanner()
        }
    }

    fun reqFindBanner2() {
        //启动一个协程
        viewModelScope.launch {
            val banner = mRepository.findBanner2()
            banner?.let {
                if (it.code == 200 && it.success) {
                    bannerInfoLiveData2.value = it.data?.toList() ?: emptyList()
                } else {
                    ToastUtils.showShort(it.message)//提示错误提示语
                    bannerInfoLiveData2.value = emptyList()//返回一个空结合
                }
            }
        }
    }

    fun reqFindBanner3() {
        //启动一个协程
        viewModelScope.launch {
            //假设这个banner是从本地获得的数据，这里为了方便就还是从网络获取
            val banner =mRepository.findBanner2()//mRepository.getBannerFromDb()
            banner?.let {
                if (it.code == 200 && it.success) {
                    bannerInfoLiveData3.value = it.data?.toList() ?: emptyList()
                } else {
                    ToastUtils.showShort(it.message)//提示错误提示语
                    bannerInfoLiveData3.value = emptyList()//返回一个空结合
                }
            }
        }
    }

    /**
     * 通过Rxhttp获取
     */
    fun reqFindBannerRxHttp() {
        viewModelScope.launch {
            mRepository.findBanner3().apply {
                onSuccess {
                    bannerInfoLiveData2.value = it
                }
                onFailure {
                    it.show()
                }
            }
        }
    }
    fun reqFindBannerRxHttp4() {
        viewModelScope.launch {
            val asyncBanner =  mRepository.findBanner4(this)
            asyncBanner.tryAwait()

            //协程并行多个请求
//            val asyncBanner = getBanners(scope) //这里返回Deferred<List<Banner>>对象
//            val asyncPersons = getStudents(scope) //这里返回Deferred<List<Student>>对象
//            val banners = asyncBanner.tryAwait()    //这里返回List<Banner>?对象
//            val students = asyncPersons.tryAwait()  //这里返回List<Student>?对象

            //协程串行多个请求
//            //获取student对象，null的话，直接返回
//            val student = getStudent() ?: return
//            //通过学生Id，查询家庭成员信息
//            val personList = getFamilyPersons(student.id)
            //拿到相关信息后，便可直接更新UI，如：
//            tvName.text = student.name



            }
        }
    }

